package com.subway.babynote.syncservice;

import java.util.List;

import org.json.JSONObject;

import com.subway.babynote.NotedbHelper;
import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecord.clientFeedRecord;
import com.subway.babynote.syncdata.ClientDataRecord.contactRecord;
import com.subway.babynote.syncdata.ClientDataRecord.feedRecord;
import com.subway.babynote.syncdata.ClientDataRecord.foodsRecord;
import com.subway.babynote.syncdata.ClientDataRecord.hwAvgRecord;
import com.subway.babynote.syncdata.ClientDataRecord.last_avgRecord;
import com.subway.babynote.syncdata.ClientDataRecord.loadinfoRecord;
import com.subway.babynote.syncdata.ClientDataRecord.tipRecord;
import com.subway.babynote.syncdata.ClientDataRecord.uxRecord;
import com.subway.babynote.syncdata.ClientDataRecord.weiboRecord;
import com.subway.babynote.syncdata.errMessage;
import weibo4andriod.*;
import weibo4andriod.http.AccessToken;

import com.subway.babynote.syncdata.ClientDataRecord.usersRecord;

import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import android.app.Activity;

public class syncapi_impl extends syncapi {
	private IBinder binder = null;
	private boolean is_bind = false;
    private SyncService bindservice = null;
    private Context parent = null;
    private boolean is_init = false;
    private boolean is_connected = false;
    private taskHelper taskh = taskHelper.gettaskHelper();
	@Override
	public int informSync(int icode,int ts) {
		// TODO Auto-generated method stub
		this.inform_service(icode, ts);
		return 0;
	}
	
	public int Init(Context context )
	{
		//bind service
		if(!this.is_init)
		{
			this.is_init = true;
			parent = context;			
			boolean b = this.bind_service();
			if(b)
			{			
				return 0;
			}
			else return -1;
		}
		else
			return 0;
	}

	@Override
	public void Destroy() {
		// TODO Auto-generated method stub
		this.unbind_service();
		this.is_init = false;
	}
	
	private ServiceConnection con = new ServiceConnection()
	{
        public void onServiceConnected(ComponentName className, IBinder service) {
            
        	try
        	{
	        	binder = service;
	        	bindservice = ((SyncService.SyncBinder)service).getService();
	        	is_connected = true;
	        	boolean bret = taskh.init(parent, bindservice.getTask());
        	}
        	catch(Exception e)
        	{
        		is_connected = false;
        	}
        }

        public void onServiceDisconnected(ComponentName className) {
        	is_connected = false;
        	bindservice = null;
        	binder = null;           
        }
	};
	
	private boolean unbind_service()
	{
		if(this.is_bind)
		{
			parent.unbindService(con);
			this.is_bind = false;
		}
		return true;
	}
	
	private boolean bind_service()
	{		
		try
		{
			Intent bind_intent = new Intent(this.parent,SyncService.class);		
			boolean b = parent.bindService(bind_intent, con, Context.BIND_AUTO_CREATE);		
			this.is_bind = true;
			return b;
		}
		catch(Exception e)
		{
			String a = e.getMessage();
			Log.i("bind_service failed:", a);
			return false;
		}
	}
	
	private boolean inform_service(int code, int ts)
	{
		boolean b = true;
		if(!this.is_connected)
			return false;
		int intput_code = syncapi.inform_code;		
    	Parcel pi = Parcel.obtain();
    	Parcel po = Parcel.obtain();    	
    	Bundle input_data = new Bundle();
    	input_data.putInt(syncapi.inform_key.code, code);
    	input_data.putInt(syncapi.inform_key.ts, ts);
    	pi.writeBundle(input_data);
    	try {
    		
			b = this.binder.transact(intput_code, pi, po, 0);
			if(b)
			{
				int iret = po.readInt();
				if(iret != 0)
					b = false;
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			String a = e.getMessage();
			Log.i("send information to service fail:", a);
			b = false;
		}
		return b;
	}

	@Override
	public int LoginUser(String username, String passwd) {
		// TODO Auto-generated method stub		
		if(!this.is_connected)
			return errMessage.err_service.errcode;
		int iret = taskh.loginUser(username, passwd);
		return iret;
	}

	@Override
	public int RegUser(usersRecord ur) {
		// TODO Auto-generated method stub
		if(!this.is_connected)
			return errMessage.err_service.errcode;
		int iret = taskh.regUser(ur);
		return iret;
	}

	@Override
	public int syncWeibo(String username, String passwd) {
		int iret = -1;
		if(uai_Sing.get_uai().isuser)
		{
			iret = taskh.syncWeibo(username, passwd);
			if(iret >= 0)
			{
				boolean bret = this.inform_service(syncapi.operation_code.syncweibo, iret);
				if(!bret)
					iret = -1;
				else
					iret = 0;
			}
		}
		else
			iret = errMessage.err_needlogin.errcode;
        return iret;
	}

	@Override
	public int Logout() {
		// TODO Auto-generated method stub
		int iret = taskh.logout();
		return iret;
	}

	@Override
	public int UnbindWeibo() {
		// TODO Auto-generated method stub
		int iret = taskh.unbindWeibo();
		return iret;
	}

	@Override
	public int AddFeed(feedRecord fr) {
		// TODO Auto-generated method stub
		return taskh.AddFeed(fr);
	}

	@Override
	public int AddVisitor(int birth, boolean gender) {
		return taskh.AddVisitor(birth, gender);
	}

	@Override
	public int AddUser(int birth, boolean gender, String email, String password) {
		return taskh.AddUser(birth, gender, email, password);
	}

	@Override
	public int AddHUser(float height) {
		
		return taskh.AddHUser(height);
	}

	@Override
	public int AddWUser(float weight) {
		// TODO Auto-generated method stub
		return taskh.AddWUser(weight);
	}

	@Override
	public int AddFeedback(String content) {
		// TODO Auto-generated method stub
		return taskh.AddFeedback(content);
	}

	@Override
	public int updateUserInfo(int birth, boolean gender) {
		// TODO Auto-generated method stub
		return taskh.updateUserInfo(birth, gender);
	}

	@Override
	public List<contactRecord> getOurContact() {
		// TODO Auto-generated method stub
		return taskh.getOurContact();
	}

	@Override
	public List<last_avgRecord> getAverage() {
		// TODO Auto-generated method stub
		return taskh.getAverage();
	}

	@Override
	public int getFoodPropid(String food_prop) {
		// TODO Auto-generated method stub
		return taskh.getFoodPropid(food_prop);
	}

	@Override
	public weiboRecord getWeibo() {
		// TODO Auto-generated method stub
		return taskh.getWeibo();
	}

	@Override
	public List<ClientDataRecord.tipRecord> getTips() {
		// TODO Auto-generated method stub
		return taskh.getTips();
	}

	@Override
	public loadinfoRecord getLoadInfo() {
		// TODO Auto-generated method stub
		return taskh.getLoadInfo();
	}

	@Override
	public usersRecord getUser() {
		// TODO Auto-generated method stub
		return taskh.getuser();
	}

	@Override
	public boolean getUserGender() {
		// TODO Auto-generated method stub
		return taskh.getUserGender();
	}

	@Override
	public int getUserAge() {
		// TODO Auto-generated method stub
		return taskh.getUserAge();
	}

	@Override
	public int getUserid() {
		// TODO Auto-generated method stub
		return taskh.getUserid();
	}

	@Override
	public int getVisitorid() {
		// TODO Auto-generated method stub
		return taskh.getVisitorid();
	}

	@Override
	public foodsRecord getFood(String foodname) {
		// TODO -generated method stub
		return taskh.getFood(foodname);
	}

	@Override
	public foodsRecord getFood(int food_id) {
		// TODO Auto-generated method stub
		return taskh.getFood(food_id);
	}

	@Override
	public hwAvgRecord getWeight(int age, boolean gender) {
		// TODO Auto-generated method stub
		return taskh.getWeight(age, gender);
	}

	@Override
	public hwAvgRecord getHeight(int age, boolean gender) {
		// TODO Auto-generated method stub
		return taskh.getHeight(age, gender);
	}

	@Override
	public List<foodsRecord> getFoodybyCategory(String category) {
		// TODO Auto-generated method stub
		return taskh.getFoodybyCategory(category);
	}

	@Override
	public List<foodsRecord> getFoodbyProp(int prop_id) {
		// TODO Auto-generated method stub
		return taskh.getFoodbyProp(prop_id);
	}

	@Override
	public List<clientFeedRecord> getSumFeeds(int date) {
		// TODO Auto-generated method stub
		return taskh.getSumFeeds(date);
	}

	@Override
	public List<clientFeedRecord> getFeeds(int date) {
		// TODO Auto-generated method stub
		return taskh.getFeeds(date);
	}

	@Override
	public uxRecord getWeightSuggestion() {
		// TODO Auto-generated method stub
		return taskh.get_w_ux();
	}

	@Override
	public uxRecord getHeightSuggestion() {
		// TODO Auto-generated method stub
		return taskh.get_h_ux();
	}

	@Override
	public uxRecord getFoodSuggestion(int food_id) {
		// TODO Auto-generated method stub
		return taskh.get_food_ux(food_id);
	}


	
	
}
